<!DOCTYPE html>
<html>
<head>
    <title>方向控制面板</title>
    <style>
        body {
            display: flex;
            justify-content: center;
            align-items: center;
            height: 100vh;
            font-family: Arial, sans-serif;
        }
        .control-panel {
            display: grid;
            grid-template-areas:
                ". up ."
                "left . right"
                ". down ."
                ". stop .";
            gap: 10px;
        }
        button {
            width: 100px;
            height: 60px;
            font-size: 16px;
            cursor: pointer;
        }
        #up { grid-area: up; }
        #down { grid-area: down; }
        #left { grid-area: left; }
        #right { grid-area: right; }

        #result {
            position: absolute;
            bottom: 20px;
            font-size: 18px;
            white-space: pre-line;
        }
    </style>
</head>
<body>
    <div class="control-panel">
        <button id="up"
                onmousedown="startAction('up')"
                onmouseup="stopAction()">UP</button>
        <button id="down"
                onmousedown="startAction('down')"
                onmouseup="stopAction()">DOWN</button>
        <button id="left"
                onmousedown="startAction('left')"
                onmouseup="stopAction()">LEFT</button>
        <button id="right"
                onmousedown="startAction('right')"
                onmouseup="stopAction()">RIGHT</button>

    </div>
    <div id="result"></div>

    <script>
        let actionInterval;
        const actionDelay = 200; // 每次动作间隔(ms)

        function startAction(direction) {
            // 立即执行一次
            executeAction(direction);
            // 设置重复执行
            actionInterval = setInterval(() => {
                executeAction(direction);
            }, actionDelay);
        }

        function stopAction() {
            clearInterval(actionInterval);
        }

        async function executeAction(direction) {
            const response = await fetch(`/button/${direction}`, {
                method: 'POST'
            });
            const data = await response.json();

            // 显示所有执行结果
            document.getElementById('result').textContent =
                data.results.join('\n');
        }
    </script>
</body>
</html>